import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { BrowserRouter } from "react-router-dom";
import { Toaster } from 'sonner';
import App from "./App.tsx";
import "./index.css";

// 初始化深色模式设置
// 在应用加载前就检查并设置深色模式
const initializeDarkMode = () => {
  try {
    const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
    const savedDarkMode = localStorage.getItem('isDark');
    const shouldBeDark = savedDarkMode !== null ? savedDarkMode === 'true' : prefersDark;
    
    if (shouldBeDark) {
      document.documentElement.classList.add('dark');
    } else {
      document.documentElement.classList.remove('dark');
    }
  } catch (error) {
    console.error('Error initializing dark mode:', error);
  }
};

// 在渲染应用前调用初始化函数
initializeDarkMode();

createRoot(document.getElementById("root")!).render(
  <StrictMode>
    <BrowserRouter>
      <App />
      <Toaster />
    </BrowserRouter>
  </StrictMode>
);
